package cn.itcast.web.controller.cargo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Pack;
import cn.itcast.domain.cargo.PackExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.util.*;

@Controller
@RequestMapping(value = "/cargo/packing", name = "装箱管理")
public class ExportPackController extends BaseController {

    @Reference
    private PackService packService;

    @Reference
    private ExportService exportService;

    @RequestMapping(value = "/toPack", name = "新增装箱界面")
    public String toPack(String id) {
        request.setAttribute("id", id);
        return "cargo/pack/pack-update";

    }

    @RequestMapping(value = "/edit", name = "新增/修改装箱")
    public String edit(Pack pack) {
        if (StrUtil.hasEmpty(pack.getPackingListId())) {
            pack.setCreateBy(getLoginUser().getId());
            pack.setCreateDept(getLoginUser().getDeptId());
            pack.setCompanyId(getCompanyId());
            pack.setCreateTime(new Date());
            packService.save(pack);
        } else {
            packService.update(pack);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/list",name = "查询装箱")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        PackExample packExample = new PackExample();
        PackExample.Criteria criteria = packExample.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        PageInfo page = packService.findByPage(pageNum, pageSize, packExample);
        request.setAttribute("page", page);
        return "cargo/pack/pack-list";

    }

    @RequestMapping(value = "/toView",name = "查看装箱单")
    public String toView(String id) {
        Pack pack = packService.findById(id);
        String[] exportIdArray = pack.getExportIds().split(",");
        List<Export> list = new ArrayList<>();
        for (String exportId : exportIdArray) {
            Export export = exportService.findById(exportId);
            list.add(export);
        }
        request.setAttribute("list",list);
        request.setAttribute("pack", pack);
        return "cargo/pack/pack-toView";
    }


    @RequestMapping(value = "/toUpdate",name = "编辑装箱单")
    public String toUpdate(String id) {
        Pack pack = packService.findById(id);
        request.setAttribute("pack", pack);
        return "cargo/pack/pack-toUpdate";
    }

    @RequestMapping(value = "/delete",name = "删除装箱单")
    public String delete(String id) {
        packService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/packPdf",name = "下载装箱单")
    public void packPdf(String id) throws  Exception{
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/packing.jasper");
        Pack pack = packService.findById(id);
        Map<String, Object> map = BeanUtil.beanToMap(pack);
        String[] exportIds = pack.getExportIds().split(",");
        List<Export> list = new ArrayList<>();
        for (String exportId : exportIds) {
            Export export = exportService.findById(exportId);
            list.add(export);
        }
        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, jrDataSource);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "装箱单.pdf");// 完成文件下载
    }

}
